#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @author: zhangbo

# @time: 2025/4/18 10:46

import time
from selenium.webdriver.common.by import By

from base.LcdOrDisplayFailureBase import LcdOrDisplayFailureBase
from common.ObjectMap import ObjectMap
from utils.start_exe import StartExe
from utils.update_conf_file import UpdateConfFile


class LcdOrDisplayFailurePage(LcdOrDisplayFailureBase):

    def click_alarm_center(self, driver):
        """
        点击报警中心
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.alarm_center())

    def click_alarm_rules(self, driver):
        """
        点击报警规则
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.alarm_rules())

    def click_base_Alarm_Rules(self, driver):
        """
        点击基础报警
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.base_Alarm_Rules())

    def lcd_input_flag(self, driver):
        """
        判断是否勾选，没勾选就点击勾选 对应报警项目
        :param driver:
        :return:
        """

        # 判断是否勾选
        result = ObjectMap().is_lcd_input_flag(driver, By.XPATH, self.input_is_selected(), self.input_span())
        if result:
            pass
        else:
            raise Exception("判断是否勾选失败")

    def update_conf_param(self):
        """
        更新conf文件
        :return:
        """
        try:
            # 接收要修改参数
            param_list = self.get_simulator_conf_parameters()

            # 修改 SimulatorConf 配置参数
            UpdateConfFile().update_conf_file(source_file_path=param_list[5], param_list=param_list)

        except Exception as e:
            print("修改conf文件失败，修改失败原因：%s" % e)

    def update_gps_info(self, source_path=None):
        """
        配置gps_info文件信息
        :param source_path: 配置的源文件
        :return:
        """
        try:
            # 更新gps_info文件
            UpdateConfFile().update_gps_info(source_path, self.get_simulator_conf_parameters()[6])
        except Exception as e:
            print("修改gps_info文件失败，修改失败原因：%s" % e)

    def start_exe_file(self, sleep_time=60):
        """
        启动exe文件
        :sleep_time: 模拟器持续时间
        :return:
        """

        try:
            start_time, end_time = StartExe().start_exe_file(exe_path=self.get_exe_path(), sleep_time=sleep_time)
            return start_time, end_time
        except Exception as e:
            print("启动exe文件失败，启动失败原因：%s" % e)

    def click_alarm_ledger(self, driver):
        """
        点击报警台账
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.get_alarm_center())

    def click_alarm_ledger_clear(self, driver):
        """
        点击报警条件清除
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.get_alarm_ledger_clear_button())

    def send_get_alarm_type(self, driver):
        """
        输入报警类型
        :param driver:
        :return:
        """
        return ObjectMap().element_fill_value(driver, By.XPATH, self.get_alarm_type(), "LCD或者显示屏故障")

    def click_get_choose_over_thermal(self, driver):
        """
        点击选择报警类型
        :param driver:
        :return:
        """
        return ObjectMap().element_click(driver, By.XPATH, self.get_choose_over_thermal())

    def send_car_num_input(self, driver):
        """
        输入车牌号，并选择
        :return:
        """

        car_number = self.car_id()

        return (ObjectMap().element_fill_value(driver, By.XPATH, self.get_car_num_input()[0], car_number, timeout=30),
                ObjectMap().element_click(driver, By.XPATH, self.get_car_num_input()[1]))

    def send_start_end_time(self, driver, start_time, end_time):
        """
        输入开始结束时间
        :param driver:
        :return:
        """
        return (ObjectMap().element_fill_value(driver, By.XPATH, self.get_start_end_time()[0], start_time, timeout=10),
                ObjectMap().element_fill_value(driver, By.XPATH, self.get_start_end_time()[1], end_time, timeout=10))

    def click_query_button(self, driver):
        """
        点击查询按钮
        :param driver:
        :return:
        """

        return ObjectMap().element_click(driver, By.XPATH, self.query_button())

    def alarm_assert_result(self, driver):
        """
        报警上报校验
        :param driver:
        :return:
        """
        try:
            driver.find_element(By.XPATH, self.result_element_tr())

            return True, "平台接收到对应的禁止驶入报警"
        except Exception as e:
            return False, "平台没有接收到报警项！请定位原因"
